package com.ytf.test.eighth;

/**
 * @author: YTF
 * @date: 2025/1/24 16:11
 * @version: 1.0
 * @description:
 */
class Super{
    public int field = 0;

    public int getField() {
        return field;
    }
}
class Sub extends Super{
    public int field = 1;

    @Override
    public int getField() {
        return field;
    }

    public int getSuperField() {
        return super.getField();
    }
}

public class FieldAccess {
    public static void main(String[] args) {
        // 向上转型
        Super sup = new Sub();
        System.out.println("sup.field = "+sup.field +
                " sup.getField() = "+sup.getField());

        Sub sub = new Sub();
        System.out.println("sub.field = "+sub.field +
                " sub.getField() = "+sub.getField()+
                " sub.getSuperField() = "+sub.getSuperField());

        /**
         * Output:
         * sup.field = 0 sup.getField() = 1
         * sub.field = 1 sub.getField() = 1 sub.getSuperField() = 0
         *
         * 总结：
         * 字段访问中父类最好设置成私有的private，不然可能会因为编译器解析导致结果和想象中不一致。
         * 这里就是两个储存空间 sup(向上转型后的实体) 和 sub
         */
    }
}
